# -*- coding: utf-8 -*-
import scrapy
import re
from gpcrawler.items import GpcrawlerItem
from scrapy.spiders import Rule
from scrapy.linkextractors.lxmlhtml import LxmlLinkExtractor

def p(value):
    print "**********************************"+str(value)

class GpSpider(scrapy.Spider):
    name = 'gp'
    # allowed_domains = ['play.google.com']
    start_urls = [
        'https://play.google.com/store/apps/details?id=com.facebook.orca',
        'https://play.google.com'
    ]

    rule = (
        Rule(LxmlLinkExtractor(
            allow=(r"https://play.google.com/store/apps/details\?id=(.*)"),
            process_value=p
        ))
    )


    def parse(self, response):
        apkId = re.findall(r'play.google.com/store/apps/details\?id=(.*)', response.url)
        if not apkId:
            return
        apkId = apkId[0]

        item = GpcrawlerItem()
        item["pkg_name"] = apkId
        item["name"] = response.xpath("//div[@class='id-app-title']/text()").extract()
        item["rate"] = response.xpath("//span[@class='rating-count']/text()").extract()
        item["category"] = response.xpath("//span[@itemprop='genre']/text()").extract()
        item["score"] = response.xpath("//div[@class='score']/text()").extract()
        item["numDownloads"] = response.xpath("//div[@itemprop='numDownloads']/text()").extract()
        item["update_time"] = response.xpath("//div[@itemprop='datePublished']/text()").extract()
        item["current_version"] = response.xpath("//div[@itemprop='softwareVersion']/text()").extract()
        item["developers"] = response.xpath("//span[@itemprop='name']/text()").extract()
        item["requires_android"] = response.xpath("//div[@itemprop='operatingSystems']/text()").extract()
        yield item


